package top.majuan.springboot3.listener;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import top.majuan.springboot3.util.IpUtils;

@Component
@Slf4j
@RequiredArgsConstructor
public class AppListener implements ApplicationListener<ApplicationReadyEvent> {
    // 获取spring的环境变量
    private final Environment environment;
    @Override
    public void onApplicationEvent(ApplicationReadyEvent event) {
        // 获取应用启动的端口号
        Integer port = environment.getProperty("server.port", Integer.class);
        String ip = IpUtils.getIpAddress();
        log.info("应用启动成功，访问地址：http://{}:{}", ip, port);

    }
}
